package com.sugar.service.oa.hr;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sugar.base.query.PageModel;
import com.sugar.domain.oa.hr.Employee;
import com.sugar.domain.system.privilege.PrivilegeRole;
import com.sugar.persistence.oa.hr.IEmployee;

@Service
public class EmployeeServiceBean implements IEmployeeService{
	
	@Resource
	private IEmployee empdao ;
	
	@Override
	public void deleteEmpService(String id) {
		
		this.empdao.deleteEmpDao(this.loadEmpByIdService(id)) ;
	}

	@Override
	public List<Employee> findEmpService(Object[] param)
			throws RuntimeException {
		
		String queryString = "select o from Employee o" ;
		
		return this.empdao.findEmpDao(queryString, null, param, null) ;
		
	}

	@Override
	public PageModel<Employee> getScorllEmpService(Object[] param,
			int firstPage, int pageSize) {
		
		String queryString = "select o from Employee o" ;
		
		return this.empdao.getScorllEmpDao(queryString, null, param, firstPage, pageSize, null);
	}

	@Override
	public Employee loadEmpByIdService(String id) {
		
		return this.empdao.loadEmpByIdDao(id) ;
	}

	@Override
	public void saveEmpService(Employee emp) {
		//emp.setEmpNo("SG-" + RandomUtil.getRandomNum(5)) ;
		
		this.empdao.saveEmpDao(emp) ;
		
		//保存员工账号
		//EmpAccount ea = emp.getAccount() ;
		//对密码进行加密
		//ea.setPassword(MD5.MD5Encode(ea.getPassword())) ;
		//ea.setEmp(emp) ;
		//this.eaccountdao.saveEmpAccountDao(ea) ;
		
		
	}

	@Override
	public void updateEmpService(Employee emp) {
		
		this.empdao.updateEmpDao(emp) ;
	}

	@Override
	public void deleteSelectEmpService(Object[] ids) {
		this.empdao.deleteSelectEmpDao(ids) ;
	}

	@Override
	public PageModel<Employee> getProcedureEmpDao() {
		this.empdao.getProcedureEmpDao() ;
		return null;
	}
	
	public void updateAssignRoleSelfEmp(String empid,int[] rids) {
		
		Employee emp = this.loadEmpByIdService(empid) ;
		
		if(null != rids && rids.length > 0) {
			Set<PrivilegeRole> roleSet = new HashSet<PrivilegeRole>() ;
			for(int i=0; i<rids.length; i++) {
				PrivilegeRole role = new PrivilegeRole() ;
				role.setR_id(rids[i]) ;
				roleSet.add(role) ;
			}
			emp.setRole(roleSet) ;
			this.updateEmpService(emp) ;
		}
		
		
	}

	@Override
	public boolean updateAssignRoleToEmpService(String empids, String roleids) {
		
		if(null != empids && !"".equals(empids) && null != roleids && !"".equals(roleids)) {
			
			String[] emps = empids.split(",") ;
			String[] roles = roleids.split(",") ;
			
			if(null != emps && emps.length > 0 && null != roles && roles.length > 0) {
				Set<PrivilegeRole> roleSet = new HashSet<PrivilegeRole>() ;
				
				for(int i=0; i<roles.length; i++) {
					PrivilegeRole role = new PrivilegeRole() ;
					role.setR_id(Integer.parseInt(roles[i])) ;
					roleSet.add(role) ;
				}
				
				for(int i=0; i<emps.length; i++) {
					Employee emp = this.loadEmpByIdService(emps[i]) ;
					emp.setRole(roleSet) ;
					
					this.empdao.updateEmpDao(emp) ;
				}
			}
		}
		return false;
	}

}
